//
//  Player.m
//  Soccer
//
//  Created by Hyun-Woong Kim on 09. 04. 29.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Player.h"
#import "ClientManager.h"
#import "Constant.h"

@implementation Player

- (id)initWithPosition:(CGPoint)pos team:(Team)team dirRad:(double)rad
{
	if (self = [super initWithPosition:pos dirRad:rad speed:6])
	{
		team_ = team;
		dirRad = rad;
		status = Normal;
	}
	return self;
}

@synthesize team=team_;
@synthesize isPlayable;
@synthesize status;
@synthesize stunDurationByFrame;
@synthesize miniMapPlayer;

- (NSString *)getFileName
{
	NSString *fileName;
	if (team_ == TeamA)
	{
		fileName = @"right_1.png";
	}
	else
	{
		fileName = @"left_blue_1.png";
	}
	
	return fileName;
}

- (void)spriteNeedsInit:(ObjectSprite *)sprite
{
	[self setNormalAnimation:sprite];
}

- (void)moveToNextPosition
{
	[super moveToNextPosition];
	// 경기장 카메라 센터 조정
	if (isPlayable)
	{
		[[[ClientManager sharedManager] match] adjustCameraPosition:position];
	}
}

- (void)spriteNeedsUpdates:(ObjectSprite *)sprite
{
	// 스턴 걸린 시점에 애니메이션 교체
	if (status == Stun)
	{
		if (stunDurationByFrame == stunDuration)
		{
			//NSLog(@"Stun start!");
			speed = speed - speedDecrease; // decrease speed
			
			// change animation to stunned one
			[self setStunnedAnimation:sprite];
		}
		
		// 스턴 종료 처리
		if(stunDurationByFrame <= 0)
		{
			// 상대방 플레이어의 스턴이 풀리게 될 때,
			// 실제로 상대방 플레이어에서는 스턴이 풀리지 않은 경우가 있다.
			// 이 경우까지 고려해줄 수 있으면 스턴 이미지 싱크 잘 맞게 될 것이다.
			status = Normal;
			speed = speed + speedDecrease;
			
			// 애니메이션 원상복귀
			[self setNormalAnimation:sprite];			
		}
		
		stunDurationByFrame--;
	}
	
	[super spriteNeedsUpdates:sprite];
	
	// animation
	if (isMoving)
	{
		int frameIndex = frameNumber++ / 5 % 5;
		if (dirRad > kPi / 2 || dirRad < -kPi / 2)
		{
			[sprite setDisplayFrame:@"left" index:frameIndex];
		}
		else
		{
			[sprite setDisplayFrame:@"right" index:frameIndex];
		}
	}
	
	if (dirRad > kPi / 2 || dirRad < -kPi / 2)
	{
		if (dirRad > kPi /2)
		{
			sprite.rotation = 180 - dirRad * kRad2Deg;
		}
		else
		{
			sprite.rotation = -180 - dirRad * kRad2Deg;
		}
	}
	else
	{
		sprite.rotation = -dirRad * kRad2Deg;
	}
	
	// minimap player 위치 업데이트
	miniMapPlayer.position = CGPointMake(position.x/10,position.y/10);
}

- (void)setNormalAnimation:(ObjectSprite *)sprite
{
	NSString *prefix;
	
	if (team_ == TeamA)
		prefix = @"left";
	else
		prefix = @"left_blue";
	
	Animation *leftAnimation = [[Animation alloc] initWithName:@"left" delay:0];
	[leftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[leftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[leftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_3.png"]];
	[leftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[leftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[sprite addAnimation:leftAnimation];
	[leftAnimation release];
	
	if (team_ == TeamA)
		prefix = @"right";
	else
		prefix = @"right_blue";
	
	Animation *rightAnimation = [[Animation alloc] initWithName:@"right" delay:0];	
	[rightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[rightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[rightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_3.png"]];
	[rightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[rightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[sprite addAnimation:rightAnimation];
	[rightAnimation release];
}

- (void)setStunnedAnimation:(ObjectSprite *)sprite
{
	NSString *prefix;
	if (team_ == TeamA)
		prefix = @"stunred_left";
	else
		prefix = @"stunblue_left";
	
	Animation *stunLeftAnimation = [[Animation alloc] initWithName:@"left" delay:0];
	[stunLeftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[stunLeftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[stunLeftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[stunLeftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[stunLeftAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[sprite addAnimation:stunLeftAnimation];
	[stunLeftAnimation release];
	
	if (team_ == TeamA)
		prefix = @"stunred_right";
	else
		prefix = @"stunblue_right";
	
	Animation *stunRightAnimation = [[Animation alloc] initWithName:@"right" delay:0];
	[stunRightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[stunRightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[stunRightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[stunRightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_2.png"]];
	[stunRightAnimation addFrameWithFilename:[prefix stringByAppendingFormat:@"_1.png"]];
	[sprite addAnimation:stunRightAnimation];
	[stunRightAnimation release];
}

@end
